package com.huiquan.vocab.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.vocab.service.VocabElementDesignateOrganService;

@Controller
public class VocabElementDesignateOrganController {

	@Autowired
	private VocabElementDesignateOrganService vocabElementDesignateOrganService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/element_designate_organ_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("checkFlag", "0");

			// 获取所有的器官
			pageParam.put("organData", vocabElementDesignateOrganService.getOrganData());

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String elementKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "elementKey");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String checkFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "checkFlag");
		String organKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "organKey");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return vocabElementDesignateOrganService.list(start, elementKey, organKey, exactSearchFlag, checkFlag,countPerPageStr);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_designate_organ.do")
	public ReturnData designateOrgan(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String organ = req.getParameter("organ").trim();

		return vocabElementDesignateOrganService.designateOrgan(id, organ);
	}

	@RequestMapping(value = "/vocab/element_designate_organ_init.htm")
	public ModelAndView init(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		vocabElementDesignateOrganService.init();

		resp.sendRedirect("/vocab/element_designate_organ_list.htm?tabid=21_10");

		return null;
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_designate_organ_submit_page.do")
	public ReturnData submitPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String ids = req.getParameter("ids");

		return vocabElementDesignateOrganService.submitPage(ids);
	}

	@RequestMapping(value = "/vocab/element_designate_organ_export.htm")
	public void export(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		vocabElementDesignateOrganService.export(resp);

	}
}
